package com.itboye.sunsun.adapter;

import java.util.Iterator;
import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.TieZiCommentAdapter.ChildViewHolder;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.AllCommontBean.CommontItemBean;
import com.itboye.sunsun.beans.LunTanCommontBean.CommontBean;
import com.itboye.sunsun.beans.LunTanCommontBean.GenTieItemBean;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ChaKanGengDuoPingLunAdapter extends BaseAdapter {

	List<CommontItemBean> dataList;
	String fid;
	String myId;
	public ChaKanGengDuoPingLunAdapter(String fid, List<CommontItemBean> dataList2) {
		this.dataList=dataList2;
		this.fid=fid;
		myId=(String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "");
	}

	@Override
	public int getCount() {
		
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		
		return null;
	}

	@Override
	public long getItemId(int position) {
		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			holder=new ViewHolder();
			convertView=LayoutInflater.from(App.ctx).inflate(R.layout.item_luntan_pinglun, null);

			holder.name=(TextView) convertView.findViewById(R.id.name);
			holder.time=(TextView) convertView.findViewById(R.id.time);
			holder.content=(TextView) convertView.findViewById(R.id.content);
			holder.head=(ImageView) convertView.findViewById(R.id.head);
			holder.delete=convertView.findViewById(R.id.delete);
			holder.delete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CommontItemBean commontBean= dataList.get((int) v.getTag());
					final String tid =commontBean.getTid();
					final String pid=commontBean.getPid();
					final String  id=commontBean.getId();
					
					MyJsonRequest.Builder<String> builder = new MyJsonRequest.Builder<String>().apiVer("100")
							.typeKey("BY_ForumPost_DeleteComment")
							.param("tid",tid)
							.param("id",id )
							.param("pid",pid)
							.param("uid", (String)SPUtils.get(App.ctx, null, SPContants.USER_ID	, ""))
							.requestListener(new XRequestListener<String>() {

								@Override
								public void onResponse(String orderBean) {

									 Toast.makeText(App.ctx, "删除成功", 1).show();
									Iterator<CommontItemBean> itr=dataList.iterator();
									while(itr.hasNext()){
										CommontItemBean bean=itr.next();
										if (bean.getTid().equals(tid)
												&&bean.getPid().equals(pid)&&bean.getId().equals(id)) {
											itr.remove();
										} 
										
									}
									notifyDataSetChanged();
									 
								}
							}).errorListener(new XErrorListener() {

								@Override
								public void onErrorResponse(Exception exception, int code, String msg) {
									 Toast.makeText(App.ctx, "删除失败", 1).show();
								}
							});

					MyJsonRequest<String> request = builder.build();
					HttpRequest.getDefaultRequestQueue().add(request);
				}
			});
			convertView.setTag(holder);
			
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		holder.delete.setTag(position);

 		CommontItemBean commontBean= dataList.get(position);

		holder.content.setText(commontBean.getComment());
		if (commontBean.getUserInfo()==null) {
			holder.name.setText(commontBean.getAuthor());
		}else{
			holder.name.setText(commontBean.getUserInfo().getNickname());
		}
		holder.time.setText(commontBean.getDatelineShow());
		
		
		if (commontBean.getUserInfo()!=null) {
			if (myId.equals(commontBean.getUserInfo().getId())) {
				holder.delete.setVisibility(View.VISIBLE);
			}else{
				holder.delete.setVisibility(View.GONE);
			}
		}else if(myId.equals(commontBean.getAuthorId())){
			holder.delete.setVisibility(View.VISIBLE);
		}else{
			holder.delete.setVisibility(View.GONE);
		}
		
		try {
			holder.head.setImageBitmap(null);
			XImageLoader.load(NetPublicConstant.IMAGE_URL+commontBean.getUserInfo().getHead()	, holder.head);
		} catch (Exception e) {
		}
		
		return convertView;
	}
	
	class ViewHolder{
		ImageView head;
		TextView name;
		TextView time;
		TextView content;
		 
		View delete;
	}

}
